package com.example.orderapplication.adapter

import android.content.Context
import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import com.example.orderapplication.R


/**
 * 左侧菜单ListView的适配器
 *
 * @author Administrator
 */
class MenuAdapter(context: Context, list: List<String>) :
    BaseAdapter() {
    private val context: Context
    private var selectItem = 0
    private val list: List<String>
    fun getSelectItem(): Int {
        return selectItem
    }

    fun setSelectItem(selectItem: Int) {
        this.selectItem = selectItem
    }

    override fun getCount(): Int {
        return list.size
    }

    override fun getItem(arg0: Int): Any {
        return list[arg0]
    }

    override fun getItemId(arg0: Int): Long {
        return arg0.toLong()
    }

    override fun getView(arg0: Int, arg1: View?, arg2: ViewGroup?): View? {
        var arg1 = arg1
        var holder: ViewHolder? = null
        if (arg1 == null) {
            holder = ViewHolder()
            arg1 = View.inflate(context, R.layout.item_menu, null)
            holder.tv_name = arg1.findViewById<View>(R.id.item_name) as TextView
            arg1.tag = holder
        } else {
            holder = arg1.tag as ViewHolder
        }
        if (arg0 == selectItem) {
            holder.tv_name!!.setBackgroundColor(Color.WHITE)
            holder.tv_name!!.setTextColor(context.resources.getColor(R.color.green))
        } else {
            holder.tv_name!!.setBackgroundColor(context.resources.getColor(R.color.background))
            holder.tv_name!!.setTextColor(context.resources.getColor(R.color.black))
        }
        holder.tv_name!!.text = list[arg0]
        return arg1
    }

    internal class ViewHolder {
        var tv_name: TextView? = null
    }

    init {
        this.list = list
        this.context = context
    }
}
